package com;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.DefaultFontMapper;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class FirstPdf {
	Document document = new Document();
	PdfWriter writer;
	File file =new File("vipul.pdf");
	
	writer = PdfWriter.getInstance(document, new FileOutputStream(file));
	document.open();

	try {
	        DefaultPieDataset pieDataset = new DefaultPieDataset();
	        pieDataset.setValue("OPT 1", 10);
	        pieDataset.setValue("OPT 2", 0);
	        pieDataset.setValue("OPT 3", 17);
	        pieDataset.setValue("OPT 4", 11);
	        JFreeChart chart = ChartFactory.createPieChart3D("Option click count", 
	                pieDataset, true, false, false);

	        final PiePlot3D plot = (PiePlot3D) chart.getPlot();
	        plot.setNoDataMessage("No data to display");
	        chart.setTitle(new TextTitle("Option Click Count"));

	        PdfContentByte pdfContentByte = writer.getDirectContent();
	        PdfTemplate pdfTemplateChartHolder = pdfContentByte.createTemplate(225,225);
	        Graphics2D graphicsChart = pdfTemplateChartHolder.createGraphics(225,225,new DefaultFontMapper());
	        Rectangle2D chartRegion =new Rectangle2D.Double(0,0,225,225);
	        chart.draw(graphicsChart,chartRegion);
	        graphicsChart.dispose();
	        pdfContentByte.addTemplate(pdfTemplateChartHolder,0,0);
	    } catch (Exception e) {
	        e.printStackTrace();
	    } 
	document.close();

}}